<template>
	<main-container>
		<!-- 搜索 -->
		<el-form :model="state.queryParams" ref="queryRef" :inline="true" v-show="showSearch">
			<el-form-item label="套餐名称">
				<el-input v-model="state.queryParams.name" clearable></el-input>
			</el-form-item>
			<el-form-item label="操作状态">
				<el-select v-model="state.queryParams.status" clearable>
					<el-option label="上架" value="1"></el-option>
					<el-option label="下架" value="0"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="initPage">
					<el-icon> <ele-Search /> </el-icon>搜索</el-button
				>
				<el-button @click="resetQuery">
					<el-icon> <ele-Refresh /> </el-icon>重置</el-button
				>
			</el-form-item>
		</el-form>
		<!-- 工具栏 -->
		<div class="hx-table-toolbar">
			<div>
				<el-button type="primary" v-auth="'upms:tenantpackage:add'" @click="add"
					><el-icon> <ele-Plus /> </el-icon>新增</el-button
				>
			</div>
			<right-toolbar :searchBtn="false" :refreshBtn="true" @search="showSearch = !showSearch" @refresh="initPage"></right-toolbar>
		</div>
		<Form ref="formRef" @initPage="initPage"></Form>
		<!-- 列表 -->
		<el-table v-loading="loading" :data="state.tableData" border>
			<el-table-column prop="name" label="套餐名称" />
			<el-table-column prop="originalPrice" label="原价（元）" />
			<el-table-column prop="salesPrice" label="销售价格（元）" />
			<el-table-column prop="status" label="状态">
				<template #default="scope">
					<dict-tag :options="status" :value="scope.row.status"></dict-tag>
				</template>
			</el-table-column>
			<el-table-column prop="appKey" label="应用系统">
				<template #default="scope">
					<dict-tag :options="application_key" :value="scope.row.appKey"></dict-tag>
				</template>
			</el-table-column>
			<el-table-column prop="createTime" label="创建时间" />
			<el-table-column label="操作" width="220" align="center">
				<template #default="scope">
					<el-button link type="primary" v-auth="'upms:tenantpackage:edit'" @click="edit(scope.row)"
						><el-icon> <ele-Edit /> </el-icon>修改</el-button
					>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页 -->
		<pagination
			v-model:total="state.page.total"
			v-model:currentPage="state.page.currentPage"
			v-model:pageSize="state.page.pageSize"
			@changePage="initPage"
		></pagination>
	</main-container>
</template>
<script setup lang="ts">
import { defineAsyncComponent, ref, reactive } from 'vue';
import { getPage } from '/@/api/upms/tenantpackage';
import { useDict } from '/@/utils/dict';
const Form = defineAsyncComponent(() => import('./form.vue'));

const state = reactive({
	queryParams: {
		name: '',
		status: '',
	},
	page: {
		total: 0,
		currentPage: 1,
		pageSize: 10,
		ascs: [],
		descs: 'create_time',
	},
	tableData: [],
});
// 字典
const { application_key, status } = useDict('application_key', 'status');
const showSearch = ref(true);
const loading = ref(false);
const formRef = ref();

const initPage = async () => {
	loading.value = true;
	let params = {
		current: state.page.currentPage,
		size: state.page.pageSize,
		ascs: state.page.ascs,
		descs: state.page.descs,
	};
	await getPage(Object.assign(params, state.queryParams))
		.then((response) => {
			state.tableData = response.data.data.records;
			state.page.total = response.data.data.total;
			loading.value = false;
		})
		.catch(() => {
			loading.value = false;
		});
};
/**
 * 新增按钮
 */
const add = () => {
	formRef.value.initForm();
};
/**
 * 修改按钮
 */
const edit = (row: any) => {
	formRef.value.initForm(row);
};
/**
 * 重置搜索表单
 */
const resetQuery = () => {
	state.queryParams = { name: '', status: '' };
};
initPage();
</script>
